import 'package:flutter/material.dart';
import 'package:yysk_app/utils/colors.dart';

class TextLinearGradientWidget extends StatelessWidget {
  final String txt;
  final Color startColor;
  final Color endColor;
  final Alignment begin;
  final Alignment end;
  final double fontSize;
  final TextOverflow overflow;
  final int? maxLines; // 新增最大行数参数
  final TextAlign? textAlign; // 新增文本对齐参数
  final FontWeight fontWeight; // 新增字体粗细参数
  final String fontFamily;

  const TextLinearGradientWidget({
    super.key,
    required this.txt,
    this.startColor = AppColors.fontColorGreenStart,
    this.endColor = AppColors.fontColorGreenEnd,
    this.begin = Alignment.topCenter,
    this.end = Alignment.bottomCenter,
    this.fontSize = 12,
    this.overflow = TextOverflow.ellipsis, // 默认改为省略号
    this.maxLines, // 可选的最大行数
    this.textAlign, // 可选的文本对齐
    this.fontWeight = FontWeight.w500, // 默认字体粗细
    this.fontFamily = ''
  });

  @override
  Widget build(BuildContext context) {
    return ShaderMask(
      shaderCallback: (Rect bounds) {
        return LinearGradient(
          colors: [startColor, endColor],
          begin: begin,
          end: end,
        ).createShader(bounds);
      },
      // 关键：添加约束确保文本能正确换行和溢出
      child: ConstrainedBox(
        constraints: const BoxConstraints(minWidth: 10, minHeight: 10),
        child: Text(
          txt,
          maxLines: maxLines, // 控制最大行数
          textAlign: textAlign, // 文本对齐方式
          overflow: overflow, // 溢出处理方式
          style: TextStyle(
            fontWeight: fontWeight,
            fontSize: fontSize,
            color: Colors.white,
            // 确保文字高度正确计算
            height: 1.2,
            fontFamily: fontFamily
          ),
        ),
      ),
    );
  }
}
